//
//  KDTabBarController.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/12.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDTabBarController.h"
#import "KDNavigationController.h"
#import "KDManageController.h"
#import "KDMessageController.h"
#import "KDWorkingController.h"
#import "KDSettingController.h"
#import "KDsubscribController.h"
#import "KDInspectController.h"
#import "KDWorkingController.h"
#import "WQMqttManager.h"
#import "SVProgressHUD.h"
#import "KDGetVpnServiceTool.h"
#import "KDHomeViewController.h"
#import "KDFocusListController.h"
@interface KDTabBarController ()<WQMqttManagerDelegate,UITabBarControllerDelegate>
@property (nonatomic, weak) WQMqttManager *manager;
@property (strong, nonatomic) NSDictionary *mqttSettings;
/**  <#Description#> */
@property (nonatomic, weak) UIButton *notyBtn;

@end

@implementation KDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate=self;
    [self setupChildController];
//    [self setupMqtt];
    
    //
    [KDGetVpnServiceTool getVpnModel];
    [self.tabBar setTintColor:KDTintColor];
    [self.tabBar setAlpha:0.3];
  
}
- (void)setupChildController{
    // 1.初始化子控制器
    
    KDHomeViewController *home = [[KDHomeViewController alloc] init];
    [self addChildVc:home title:@"首页" image:@"设置" selectedImage:@"设置_sl"];
    
    KDFocusListController *focus = [[KDFocusListController alloc] init];
    [self addChildVc:focus title:@"我的关注" image:@"设置" selectedImage:@"设置_sl"];
    
    //KDManageController *home = [[KDManageController alloc] init];
    //[self addChildVc:home title:@"管理" image:@"管理" selectedImage:@"管理_sl"];
    
//    KDMessageController *messageCenter = [[KDMessageController alloc] init];
//    [self addChildVc:messageCenter title:@"消息" image:@"消息" selectedImage:@"消息_sl"];
    KDInspectController *inspect = [[KDInspectController alloc] init];
    [self addChildVc:inspect title:@"巡查" image:@"巡查" selectedImage:@"巡查_sl"];
//    KDWorkingController *working = [[KDWorkingController alloc] init];
//    [self addChildVc:working title:@"工作" image:@"工作" selectedImage:@"工作_sl"];
    
    KDSettingController *setting = [[KDSettingController alloc] init];
    [self addChildVc:setting title:@"设置" image:@"设置" selectedImage:@"设置_sl"];
    
    
}
- (void)setupMqtt{
    WQMqttManager *manager = [WQMqttManager sharedWQMqttManager];
    manager.delegate = self;
    self.manager = manager;
    [manager connectToHost];
    [manager.manager addObserver:self
                      forKeyPath:@"state"
                         options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                         context:nil];

}
/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    KDNavigationController *nav = [[KDNavigationController alloc] initWithRootViewController:childVc];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    nav.navigationItem.backBarButtonItem = backItem;
    // 添加为子控制器
    [self addChildViewController:nav];
}

#pragma mark - WQMqttManagerDelegate
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    switch (self.manager.manager.state) {
        case MQTTSessionManagerStateClosed:
            
            KDLog(@"MQTTSessionManagerStateClosed");
            break;
        case MQTTSessionManagerStateClosing:
            
            KDLog(@"MQTTSessionManagerStateClosing");
            break;
        case MQTTSessionManagerStateConnected:
             KDLog(@"MQTTSessionManagerStateConnected");
            
            break;
        case MQTTSessionManagerStateConnecting:
            
            KDLog(@"MQTTSessionManagerStateConnecting");
            break;
        case MQTTSessionManagerStateError:
            
            KDLog(@"MQTTSessionManagerStateError");
            break;
        case MQTTSessionManagerStateStarting:
            KDLog(@"MQTTSessionManagerStateStarting");
            break;
        default:
            
            KDLog(@"not connected");
            break;
    }
}
- (void)receiveData:(NSData *)data ontopic:(NSString *)topic{
    
    NSDictionary *dataDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    //    NSString *senderString = [topic substringFromIndex:self.base.length + 1];
    NSString *message = [NSString stringWithFormat:@"收到通知:\n%@", dataDic[@"content"]] ;
    [SVProgressHUD showInfoWithStatus:message maskType:SVProgressHUDMaskTypeGradient];
    [UIView animateWithDuration:1.0 animations:^{
        self.notyBtn.hidden = NO;
    }completion:^(BOOL finished) {
        self.notyBtn.hidden = YES;
    }];
//    [self.chat insertObject:message atIndex:0];
//    [self.tableView reloadData];
    
    KDLog(@"%s", __func__);
    if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
        KDLog(@"在后台");
        UILocalNotification *localNoty = [[UILocalNotification alloc]init];
        localNoty.alertBody = message;
        localNoty.fireDate = [NSDate date];
        localNoty.soundName = @"default";
        [[UIApplication sharedApplication]scheduleLocalNotification:localNoty];
        NSInteger num = [UIApplication sharedApplication].applicationIconBadgeNumber;
        num++;
        [UIApplication sharedApplication].applicationIconBadgeNumber = num;
        [self setSelectedIndex:2];
        //发送通知给订阅界面
        [[NSNotificationCenter defaultCenter] postNotificationName:@"KDSubscribGetPushNoty" object:nil userInfo:@{@"type":dataDic[@"type"]}];
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [self removeObserver];
    KDLog(@"%s", __func__);
//    [self.manager disconnect];
}
- (void)removeObserver{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//
//    
//    [self.manager.manager removeObserver:self forKeyPath:@"state" context:nil];

}
- (void)dealloc{
    
//    [self.manager.manager removeObserver:self forKeyPath:@"state" context:nil];

}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
        
    
    if (tabBarController.selectedIndex==1) {
        UINavigationController *navigation =(UINavigationController *)viewController;
        KDFocusListController *notice=(KDFocusListController *)navigation.topViewController;
        [notice refreshData];
    }
}
@end
